#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <cstring>

using namespace std;

#define PRODUCTION_MAX 512
#define ITEM_MAX 128
#define VN_MAX 128
#define VT_MAX 128
#define STRING_MAX 1024

/**
 * used to index the sorted productions and items
 * @ptr:    means the subscript of production[] or item[]
 * @length: means the same type size
**/
struct Index
{
  int ptr;
  int size;
  Index (): ptr(-1), size(0) { }
};

struct Production
{
  int A;
  int B;
  int C;
};

struct Item
{
  int A;
  char B;
};

/**
 * deprecated
 * tree[i][j][k] = {x, y}
 * means in string[i][j] used kth in index to reduce to A(i or p)'s count is y
 * @root:   means
 * @num:    means
**/
struct Tree
{
  int root;
  unsigned num;

  Tree (): root(-1), num(-1) { }
  Tree (int root, unsigned num): root(root), num(num) { }
};

// index of Item by production.B and production.C
Index p_idx[VN_MAX][VN_MAX];

// index of Item by item.B
Index i_idx[VT_MAX];

Production production[PRODUCTION_MAX];

Item item[ITEM_MAX];

unsigned int my_tree[STRING_MAX][STRING_MAX][VN_MAX];

int vn_num;
int production_num;
int item_num;
int string_length;
char str[STRING_MAX];

void update(int len);
void slide(int l, int r);

int main(int argc, char **argv)
{
  // basic io
  if (argc != 2)
  { printf ("cyk <input.txt>"); return 0; }

  freopen(argv[1], "r", stdin);
  scanf("%d\n", &vn_num); scanf("%d\n", &production_num);
  for (int i = 0; i < production_num; i++)
    scanf("<%d>::=<%d><%d>\n", &production[i].A, &production[i].B, &production[i].C);
  scanf("%d\n", &item_num);
  for (int i = 0; i < item_num; i++)
    scanf("<%d>::=%c\n", &item[i].A, &item[i].B);
  scanf("%d\n", &string_length); scanf("%s\n", str);

  // sort ascending order by item.B and item.A
  sort (item, item + item_num, [](const Item& a,
                                  const Item& b)
  { return a.B == b.B ? a.A < b.A : a.B < b.B; });

  // init the index which indexed by [item.B]
  for (int i = 0; i < item_num; i++)
  {
    int t = item[i].B;
    if (i_idx[t].ptr == -1)
      i_idx[t].ptr = i;
    i_idx[t].size++;
  }

  // sort ascending order by production.B, production.C and production.A
  sort (production, production + production_num, [](const Production& a,
                                                    const Production& b)
  { return a.B == b.B ? (a.C == b.C ? a.A < b.A : a.C < b.C) : a.B < b.B; });

#ifdef __DEBUG__
  printf ("line: %d\n", __LINE__);
  for (int i = 0; i < production_num; i++)
    printf ("%d -> %d %d\n", production[i].A, production[i].B, production[i].C);
  puts ("--------------------------------");
#endif

  // init the index which indexed by [production.B][production.C]
  for (int i = 0; i < production_num; i++)
  {
    int n1 = production[i].B;
    int n2 = production[i].C;
    if (p_idx[n1][n2].ptr == -1) p_idx[n1][n2].ptr = i;
    p_idx[n1][n2].size++;
  }

  for (int i = 0; i < string_length; i++)
  {
    int t = str[i];
    for (int j = i_idx[t].ptr; j < i_idx[t].ptr + i_idx[t].size; j++)
      my_tree[i][i][item[j].A] = 1;
  }

  for (int len = 2; len <= string_length; len++)
    update (len);

  printf("%u\n", my_tree[0][string_length - 1][0]);
  return 0;
}

void update(int len)
{
  for (int l = 0, r = l + len - 1; l <= string_length - len; l++, r++)
    slide(l, r);
}

void slide(int l, int r)
{
  for (int mid = l + 1; mid <= r; mid++)
  {
    for (int i = 0; i < vn_num; i++)
    {
      for (int j = 0; j < vn_num; j++)
      {
        if (my_tree[l][mid - 1][i] != 0 && my_tree[mid][r][j] != 0
          && p_idx[i][j].size != -1)
        {
          int begin = p_idx[i][j].ptr;
          int end = begin + p_idx[i][j].size;

          for (int p_idx = begin; p_idx < end; p_idx++)
          {
            my_tree[l][r][production[p_idx].A] += my_tree[l][mid - 1][i] * my_tree[mid][r][j];
          }
        }
      }
    }
  }
}
